//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.wills.PssProductSaleDao;
import com.aidong.dao.wills.PssProductSaleListDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.model.wills.PayList;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssProductSale;
import com.aidong.model.wills.PssProductSaleList;
import com.aidong.service.PssPrintDataService;
import com.aidong.util.StockUtil;
import com.aidong.web.controller.pss.PopProductController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"productSaleForMinus"})
public class ProductSaleForMinusController extends PopProductController {
    @Resource
    PssProductSaleDao pssProductSaleDao;
    @Resource
    PssProductSaleListDao pssProductSaleListDao;
    @Resource
    PssProductStockDao pssProductStockDao;
    @Resource
    StockUtil stockUtil;
    @Resource
    PssPrintDataService pssprintDataService;

    public ProductSaleForMinusController() {
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PssProductSale pssProductSale = new PssProductSale();
        pssProductSale.setShopCode(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("pss/productsaleforminus/edit");
        mv.addObject("pssProductSale", pssProductSale);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("shopCode", this.getLoginShopCode());
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("allowType", this.getTypeList("allowType"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssProductSale") PssProductSale pssProductSale, @RequestParam(value = "money",required = false) String money, @RequestParam("payls") String payls, Products products) {
        if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请选择要销售的商品");
        } else {
            try {
                Date e = new Date();
                pssProductSale.setShopCode(this.getLoginShopCode());
                pssProductSale.setSaleDate(e);
                pssProductSale.setCardCode("");
                pssProductSale.setMemberCode("");
                pssProductSale.setSaleAmount(Double.valueOf(Double.parseDouble(money)));
                pssProductSale.setCreateUser(this.getLoginUserCode());
                pssProductSale.setCreateTime(e);
                pssProductSale.setUpdateTime(e);
                PssProductSaleList pss = null;
                ArrayList lst = new ArrayList();
                Iterator payList = products.getLstProduct().iterator();

                while(payList.hasNext()) {
                    PssPopProduct payMsg = (PssPopProduct)payList.next();
                    pss = new PssProductSaleList();
                    if(!this.chkNull(payMsg.getProductCode())) {
                        if(!this.stockUtil.checkStock(this.getLoginShopCode(), payMsg.getProductCode(), payMsg.getQty())) {
                            return this.getResult(false, "请确认商品：" + payMsg.getProductCode() + "的库存！");
                        }

                        pss.setShopCode(this.getLoginShopCode());
                        pss.setProductCode(payMsg.getProductCode());
                        pss.setPrice(payMsg.getAllowancePrice());
                        pss.setQuantity(payMsg.getQty());
                        pss.setMemo(payMsg.getMemo());
                        pss.setCreateUser(this.getLoginUserCode());
                        pss.setCreateTime(e);
                        pss.setUpdateTime(e);
                        lst.add(pss);
                    }
                }

                pssProductSale.setEarnestId((Integer)null);
                pssProductSale.setEarnestAmount(Double.valueOf(0.0D));
                ArrayList payList1 = new ArrayList();
                StringBuilder payMsg1 = new StringBuilder();
                PayList pay = this.getPayList(payls, payList1, "03", payMsg1);
                if(pay == null) {
                    return this.getResult(false, payMsg1.toString());
                }

                if(Double.compare(pay.getAmount().doubleValue(), Double.parseDouble(money)) != 0) {
                    return this.getResult(false, "付款金额不一致");
                }

                pssProductSale.setSaleCode(pay.getSalesCode());
                this.pssProductSaleDao.addSale(pssProductSale, lst, this.pssProductStockDao, payList1);
                this.addUserLog("销售", "销售单号:" + pssProductSale.getAutoId());
            } catch (Exception var11) {
                this.log.error("add pssProductSale", var11);
                return this.getResult(false, "销售发生错误");
            }

            return this.getResult(true, "销售成功");
        }
    }

    @RequestMapping({"print"})
    private ModelAndView print(@RequestParam(value = "money",required = false) String money, @RequestParam("payls") String payls, Products products) {
        StringBuilder msg = new StringBuilder();
        ArrayList payList = new ArrayList();
        StringBuilder payMsg = new StringBuilder();
        PayList pay = this.getPayList(payls, payList, "03", payMsg);
        if(pay == null) {
            return this.showAlert(payMsg.toString());
        } else {
            String data = this.pssprintDataService.getPssSaleData(((PayList)payList.get(0)).getShopCode(), Double.valueOf(Double.parseDouble(money)), pay.getAmount(), ((PayList)payList.get(0)).getPayType(), pay.getSalesCode(), products, msg);
            if(data == null) {
                return this.showAlert("销售打印发生错误");
            } else {
                ModelAndView mv = new ModelAndView("pss/print/print");
                mv.addObject("data", data);
                mv.addObject("title", "销售单打印");
                mv.addObject("ptjs", "psssale");
                return mv;
            }
        }
    }
}
